package com.logic.landseaserver.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.logic.landseaserver.common.LandseaErrorMessageEnum;
import com.logic.landseaserver.common.exception.LandseaException;
import com.logic.landseaserver.models.amortization.AmortizationCalculate;

public class DateUtil
{
    private static DateUtil util;
    
    private static SimpleDateFormat dateSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    
    private static SimpleDateFormat timeSimpleDateFormat = new SimpleDateFormat("HH:mm:ss");
    
    private static SimpleDateFormat datetimeSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
    private static SimpleDateFormat orderDateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
    
    private static SimpleDateFormat dateymdDateFormat = new SimpleDateFormat("yyyyMMdd");
    
    private static SimpleDateFormat dateSimpleDateFormat2 = new SimpleDateFormat("yyyy/MM/dd");
    
    private static SimpleDateFormat webankDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
    
    private static SimpleDateFormat dateYMSimpleDateFormat = new SimpleDateFormat("yyyyMM");
    
    private static SimpleDateFormat datetimeSimpleDateFormatYMDHM = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    
    private static SimpleDateFormat dateDDSimpleDateFormat = new SimpleDateFormat("dd");
    
    private static SimpleDateFormat dateMMSimpleDateFormat = new SimpleDateFormat("MM");
    
    private static SimpleDateFormat monthSimpleDateFormat = new SimpleDateFormat("yyyy-MM");
    
    private static SimpleDateFormat yearSimpleDateFormat = new SimpleDateFormat("yyyy");
    
    private static final Logger LOG = LoggerFactory.getLogger(DateUtil.class);
    
    public Date getNextYearTime()
    {
        return DateUtils.addYears(new Date(), 1);
    }
    
    public Date getLastMonthTime(Date d)
    {
        return DateUtils.addDays(d, -getDaysOfMouth(d));
    }
    
    public Date getNextMonthTime(Date d)
    {
        return DateUtils.addDays(d, getDaysOfMouth(d));
    }
    
    public Date getNextDayTime(Date d)
    {
        return DateUtils.addDays(d, 1);
    }
    
    public Date getLastSevenDayTime(Date d)
    {
        return DateUtils.addDays(d, -7);
    }
    
    public Date getNextSevenDayTime(Date d)
    {
        return DateUtils.addDays(d, 7);
    }
    
    public Date getNextMinute(int minutes)
    {
        return DateUtils.addMinutes(new Date(), minutes);
    }
    
    public static DateUtil getInstence()
    {
        if (util == null)
        {
            util = new DateUtil();
        }
        return util;
    }
    
    public static String convertDate(Date d)
    {
        if (d == null)
        {
            return "";
        }
        return dateSimpleDateFormat.format(d);
    }
    
    public static Date convertDate(String s)
    {
        if (s == null)
        {
            return null;
        }
        try
        {
            return dateSimpleDateFormat.parse(s);
        }
        catch (ParseException e)
        {
            return null;
        }
    }
    
    public static Date dateConvertDate(Date d)
    {
        return convertDate(convertDate(d));
    }
    
    public static String convertYMDDate(Date d)
    {
        if (d == null)
        {
            return "";
        }
        return dateymdDateFormat.format(d);
    }
    
    public Date convertYMDDate(String s)
    {
        if (s == null)
        {
            return null;
        }
        try
        {
            return dateymdDateFormat.parse(s);
        }
        catch (ParseException e)
        {
            return null;
        }
    }
    
    public Date convertDate2(String s)
    {
        if (s == null)
        {
            return null;
        }
        try
        {
            return dateSimpleDateFormat2.parse(s);
        }
        catch (ParseException e)
        {
            return null;
        }
    }
    
    public String convertDate2(Date d)
    {
        if (d == null)
        {
            return "";
        }
        return dateSimpleDateFormat2.format(d);
    }
    
    public String convertDate3(Date d)
    {
        if (d == null)
        {
            return "";
        }
        return dateSimpleDateFormat.format(d);
    }
    
    public String convertTime(Date d)
    {
        if (d == null)
        {
            return "";
        }
        return timeSimpleDateFormat.format(d);
    }
    
    public static String convertWeBankTime(Date d)
    {
        if (d == null)
        {
            return "";
        }
        return webankDateFormat.format(d);
    }
    
    public String orderDateFormat(Date d)
    {
        if (d == null)
        {
            return "";
        }
        return orderDateFormat.format(d);
    }
    
    public Date convertTime(String s)
    {
        if (s == null)
        {
            return null;
        }
        try
        {
            return timeSimpleDateFormat.parse(s);
        }
        catch (ParseException e)
        {
            return null;
        }
    }
    
    public static String convertDateTime(Date d)
    {
        if (d == null)
        {
            return "";
        }
        return datetimeSimpleDateFormat.format(d);
    }
    
    public static Date convertDateTime(String s)
    {
        if (s == null)
        {
            return null;
        }
        try
        {
            return datetimeSimpleDateFormat.parse(s);
        }
        catch (ParseException e)
        {
            return null;
        }
    }
    
    public String convertYMDHM(Date d)
    {
        if (d == null)
        {
            return "";
        }
        return datetimeSimpleDateFormatYMDHM.format(d);
    }
    
    public Date convertYMDHM(String s)
    {
        if (s == null)
        {
            return null;
        }
        try
        {
            return datetimeSimpleDateFormatYMDHM.parse(s);
        }
        catch (ParseException e)
        {
            return null;
        }
    }
    
    public String convert(Date d, String pattern)
    {
        if (d == null)
        {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(d);
    }
    
    public Date convert(String s, String pattern)
    {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try
        {
            return sdf.parse(s);
        }
        catch (ParseException e)
        {
            return null;
        }
    }
    
    public boolean compareNow(String s)
    {
        Date oldDate;
        try
        {
            oldDate = dateSimpleDateFormat.parse(s);
        }
        catch (ParseException e)
        {
            return false;
        }
        Date d2 = new Date(); // 第二个时间
        int d1Number = Integer.parseInt(dateymdDateFormat.format(oldDate).toString());
        int d2Number = Integer.parseInt(dateymdDateFormat.format(d2).toString());
        if (d1Number >= d2Number)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    public String convertMonth(Date d)
    {
        if (d == null)
        {
            return "";
        }
        return monthSimpleDateFormat.format(d);
    }
    
    public String convertYear(Date d)
    {
        if (d == null)
        {
            return "";
        }
        return yearSimpleDateFormat.format(d);
    }
    
    /**
     * 验证时间是否在范围内容
     *
     * @param d1 用户输入的时间
     * @param d2 可入住日期 + 7
     * @return
     */
    public boolean compareDate(Date d1, Date d2)
    {
        // d1 用户输入的时间
        if (d1.compareTo(d2) == 0)
        {
            return true;
        }
        else
        {
            Date d2End = DateUtils.addDays(d2, Calendar.DAY_OF_WEEK);// 13---20 好
            if (d1.compareTo(d2End) == 0)
            {
                return true;
            }
            if (d1.before(d2End) && d1.after(d2))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
    
    public boolean compareSevenDays(String s)
    {
        Date oldDate;
        try
        {
            oldDate = dateSimpleDateFormat.parse(s);
        }
        catch (ParseException e)
        {
            return false;
        }
        Date d2 = DateUtils.addDays(oldDate, 7);
        int d1Number = Integer.parseInt(dateymdDateFormat.format(oldDate).toString());
        int d2Number = Integer.parseInt(dateymdDateFormat.format(d2).toString());
        if (d1Number >= d2Number)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    /**
     * 可入住日期 1.如果日期为空，则默认为当前日期顺延7天内可租,可入住日期为当前日期
     *
     * @param avbDate
     * @return
     * @author junny
     */
    public String getAvailableDate(String avbDate)
    {
        String avbDateNew = "";
        if (StringUtils.isEmpty(avbDate))
        {
            avbDateNew = DateUtil.getInstence().convertDate2(new Date());
        }
        return avbDateNew;
    }
    
    /**
     * 获取当月一共有多少天
     *
     * @return
     */
    public int getDaysOfMouth(Date startTime)
    {
        Calendar aCalendar = Calendar.getInstance();
        aCalendar.setTime(startTime);
        int day = aCalendar.getActualMaximum(Calendar.DATE);
        return day;
    }
    
    /**
     * 获取今天是哪一天
     *
     * @return
     */
    public int getNowDaysOfMouth(Date startTime)
    {
        Calendar aCalendar = Calendar.getInstance();
        aCalendar.setTime(startTime);
        int nowDay = aCalendar.get(Calendar.DAY_OF_MONTH);
        return nowDay;
    }
    
    /**
     * 获取当前月还有多少天
     *
     * @return
     */
    public int getDaysOfMouthHave(Date startTime)
    {
        Calendar aCalendar = Calendar.getInstance();
        aCalendar.setTime(startTime);
        int nowDay = aCalendar.get(Calendar.DAY_OF_MONTH);
        int day = aCalendar.getActualMaximum(Calendar.DATE);
        return (day - nowDay) + 1;
    }
    
    /**
     * 根据起始时间+月份获取结束时间的上个月最后一天
     *
     * @param startTime
     * @param month
     * @return 结束时间的上个月最后一天
     */
    public String getEndTime(Date startTime, int month)
    {
        /*
         * if (month > 0 && startTime != null) { month = month + 2; } else { month = 2; }
         */
        Date endDate = DateUtils.addMonths(startTime, month + 1);
        Calendar calendar = Calendar.getInstance();
        
        calendar.setTime(endDate);
        
        calendar.set(Calendar.DAY_OF_MONTH, 0);
        return dateSimpleDateFormat2.format(calendar.getTime());
    }
    
    /**
     * 根据时间取这个月的第一天
     *
     * @param startTime
     * @return
     */
    public Date getFirstDayOfMonth(Date startTime)
    {
        // 获取前月的第一天
        Calendar cal = Calendar.getInstance();// 获取当前日期
        cal.setTime(startTime);
        cal.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
        Date firstDay = cal.getTime();
        return firstDay;
    }
    
    public Date getLastDayOfMonth(Date nowDate)
    {
        Calendar ca = Calendar.getInstance();
        ca.setTime(nowDate);
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date lasttDay = ca.getTime();
        return lasttDay;
    }
    
    public String convertYM(Date d)
    {
        if (d == null)
        {
            return "";
        }
        return dateYMSimpleDateFormat.format(d);
    }
    
    public Date convertYM(String ymd)
    {
        if (ymd == null)
        {
            return null;
        }
        try
        {
            return dateYMSimpleDateFormat.parse(ymd);
        }
        catch (ParseException e)
        {
            return null;
        }
    }
    
    /**
     * 根据时间取下个月的第一天
     *
     * @param startTime
     * @return
     */
    public Date getFirstDayOfLastMonth(Date startTime)
    {
        Calendar c = Calendar.getInstance();
        c.setTime(startTime);
        c.set(Calendar.MONTH, c.get(Calendar.MONTH) + 1);
        c.set(Calendar.DAY_OF_MONTH, 1);
        return c.getTime();
    }
    
    /**
     * 根据时间取下个月的第一天
     *
     * @param startTime
     * @return
     */
    public String getFirstDayOfLastMonthStr(Date startTime)
    {
        Calendar c = Calendar.getInstance();
        c.setTime(startTime);
        c.set(Calendar.MONTH, c.get(Calendar.MONTH) + 1);
        c.set(Calendar.DAY_OF_MONTH, 1);
        return dateSimpleDateFormat.format(c.getTime());
    }
    
    /**
     * 根据时间取下个月的最后一天
     *
     * @param startTime
     * @return
     */
    public Date getLastDayOfNextMonth(Date startTime)
    {
        Calendar c = Calendar.getInstance();
        c.setTime(startTime);
        c.set(Calendar.MONTH, c.get(Calendar.MONTH) + 1);
        c.set(Calendar.DAY_OF_MONTH, 1);
        c.set(Calendar.MONTH, c.get(Calendar.MONTH) + 1);
        c.set(Calendar.DAY_OF_MONTH, 0);
        return c.getTime();
    }
    
    /**
     * 根据时间取上个月的最后一天
     *
     * @param startTime
     * @return
     */
    public Date getLastDayOfLastMonth(Date startTime)
    {
        Calendar c = Calendar.getInstance();
        c.setTime(startTime);
        c.set(Calendar.MONTH, c.get(Calendar.MONTH));
        c.set(Calendar.DAY_OF_MONTH, 0);
        return c.getTime();
    }
    
    /**
     * 取下个月第26天
     *
     * @param startTime
     * @return
     */
    public Date get26DayOfThisMonth(Date startTime)
    {
        // 获取前月的第一天
        Calendar cal = Calendar.getInstance();// 获取当前日期
        cal.setTime(startTime);
        cal.set(Calendar.MONTH, cal.get(Calendar.MONTH));
        cal.set(Calendar.DAY_OF_MONTH, 26);
        return cal.getTime();
    }
    
    /**
     * 根据时间取下个月的第一天
     *
     * @param startTime
     * @return
     */
    public Date get26DayOfLastMonth(Date startTime)
    {
        Calendar c = Calendar.getInstance();
        c.setTime(startTime);
        c.set(Calendar.MONTH, c.get(Calendar.MONTH) + 1);
        c.set(Calendar.DAY_OF_MONTH, 26);
        return c.getTime();
    }
    
    public int monthBetween(Date startTime, Date endTime)
    {
        int months = (endTime.getYear() - startTime.getYear()) * 12 + (endTime.getMonth() - startTime.getMonth());
        return months;
    }
    
    public boolean isSameDate(Date date1, Date date2)
    {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        boolean isSameYear = cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR);
        boolean isSameMonth = isSameYear && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);
        boolean isSameDate = isSameMonth && cal1.get(Calendar.DAY_OF_MONTH) == cal2.get(Calendar.DAY_OF_MONTH);
        return isSameDate;
    }
    
    public Date getNextDay0h(Date nowDate)
    {
        Date date = null;
        SimpleDateFormat format = null;
        Calendar myDate = Calendar.getInstance();
        myDate.setTime(nowDate);
        myDate.add(Calendar.DAY_OF_MONTH, 1);
        date = myDate.getTime();
        format = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        String rtnYes = format.format(date);
        return this.convertDateTime(rtnYes);
    }
    
    /**
     * 取特定哪一天
     *
     * @param startTime
     * @return
     */
    public Date getDayOfMonth(Date startTime, int days)
    {
        Calendar cal = Calendar.getInstance();// 获取当前日期
        cal.setTime(startTime);
        cal.set(Calendar.MONTH, cal.get(Calendar.MONTH));
        cal.set(Calendar.DAY_OF_MONTH, days);
        return cal.getTime();
    }
    
    public Date getAppointDay(int type, int n)
    {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(type, n);
        return calendar.getTime();
    }
    
    // 求日期相差天数，
    public static int differentDaysByMillisecond(Date date1, Date date2)
    {
        Date start = DateUtil.getInstence().convertDate(DateUtil.getInstence().convertDate(date1));
        Date end = DateUtil.getInstence().convertDate(DateUtil.getInstence().convertDate(date2));
        int days = (int)((end.getTime() - start.getTime()) / (1000 * 3600 * 24));
        return days + 1;
    }
    
    public static int compareDate(String date1, String date2)
    {
        Calendar bef = Calendar.getInstance();
        Calendar aft = Calendar.getInstance();
        try
        {
            bef.setTime(dateYMSimpleDateFormat.parse(date1));
            aft.setTime(dateYMSimpleDateFormat.parse(date2));
        }
        catch (ParseException e)
        {
            LOG.error("compareDate error.", e);
        }
        int result = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH);
        int month = (aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR)) * 12;
        return Math.abs(month + result);
    }
    
    public static int getApartMonth(Date date1, Date date2)
    {
        Calendar bef = Calendar.getInstance();
        Calendar aft = Calendar.getInstance();
        String s1 = dateYMSimpleDateFormat.format(date1);
        String s2 = dateYMSimpleDateFormat.format(date2);
        try
        {
            bef.setTime(dateYMSimpleDateFormat.parse(s1));
            aft.setTime(dateYMSimpleDateFormat.parse(s2));
        }
        catch (ParseException e)
        {
            LOG.error("getApartMonth error.", e);
        }
        int result = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH);
        int month = (aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR)) * 12;
        return Math.abs(month + result);
    }
    
    /**
     * [简要描述]：比较date1和date2</br>
     * [详细描述]：date1大于date2返回true否则返回false</br>
     * [作者]：Aaron(2017-06-27)</br>
     *
     * @param date1 日期1
     * @param date2 日期2
     * @return true或false
     */
    public static boolean compareLongDate(long date1, long date2)
    {
        return date1 > date2;
    }
    
    /**
     * [简要描述]：获取当前时间Unix的时间戳</br>
     * [详细描述]：Unix精确到秒，而System.currentTimeMillis()精确到毫秒</br>
     * [作者]：Aaron(2017-06-28)</br>
     *
     * @return 当前日期的时间戳
     */
    public static int currentUnixTimeStamp()
    {
        return (int)(System.currentTimeMillis() / 1000);
    }
    
    /**
     * 获取精确到秒的时间戳
     *
     * @param date
     * @return
     */
    public static int getSecondTimestamp(Date date)
    {
        if (null == date)
        {
            return 0;
        }
        String timestamp = String.valueOf(date.getTime() / 1000);
        return Integer.valueOf(timestamp);
    }
    
    /**
     * [简要描述]：根据Unix时间戳获取当前时间</br>
     * [详细描述]：返回yyyy-MM-dd</br>
     * [作者]：Aaron(2017-07-04)</br>
     *
     * @param timestemp Unix时间戳
     * @return 时间字符串
     */
    public static String getDateByUnixTimestemp(int timestemp)
    {
        long longTimestemp = timestemp;
        Date date = new Date(longTimestemp * 1000);
        return dateSimpleDateFormat.format(date);
    }
    
    /**
     * [简要描述]：根据Unix时间戳获取当前时间</br>
     * [详细描述]：返回日期</br>
     * [作者]：Aaron(2017-07-04)</br>
     *
     * @param timestemp Unix时间戳
     * @return 日期对象
     */
    public static Date getDateByUnixTimestemp2(int timestemp)
    {
        long longTimestemp = timestemp;
        Date date = new Date(longTimestemp * 1000);
        return date;
    }
    
    /**
     * [简要描述]：获取格式化时间</br>
     * [详细描述]：</br>
     * [作者]：Aaron(2017-07-06)</br>
     *
     * @param dateStr 时间字符串
     * @param format 时间格式
     * @return 格式化后的Date
     * @throws LandseaException
     */
    public static Date getFormatDate(String dateStr, String format)
        throws LandseaException
    {
        SimpleDateFormat dateSimpleDateFormat = new SimpleDateFormat(format);
        try
        {
            return dateSimpleDateFormat.parse(dateStr);
        }
        catch (ParseException e)
        {
            LOG.error("getFormatDate|Parse failed|dateStr=" + dateStr + "|format=" + format, e);
            throw LandseaException.createException(LandseaErrorMessageEnum.COMMON_GET_FORMAT_DATE_ERROR, e);
        }
    }
    
    /**
     * 获取日期 dd
     *
     * @param d 日期
     * @return
     */
    public String getDd(Date d)
    {
        if (d == null)
        {
            return "";
        }
        return dateDDSimpleDateFormat.format(d);
    }
    
    public String getMm(Date d)
    {
        if (d == null)
        {
            return "";
        }
        return dateMMSimpleDateFormat.format(d);
    }
    
    public static int compareTime(Date d1, Date d2)
    {
        
        Date start = DateUtil.getInstence().convertDate(DateUtil.getInstence().convertDate(d1));
        Date end = DateUtil.getInstence().convertDate(DateUtil.getInstence().convertDate(d2));
        
        if (start.getTime() > end.getTime())
            // d1 在d2之后
            return 1;
        else if (start.getTime() < end.getTime())
            // d1在d2之前
            return -1;
        else
            // 相等
            return 0;
    }
    
    /**
     * [简要描述]：获取下个月的第day天的hour时minute分second秒</br>
     * [详细描述]：返回日期格式</br>
     * [作者]：Aaron(2017-07-18)</br>
     * @return
     */
    public static Date getDayOfNextMonth(int day, int hour, int minute, int second)
    {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 1);
        calendar.set(Calendar.DAY_OF_MONTH, day);
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minute);
        calendar.set(Calendar.SECOND, second);
        return calendar.getTime();
    }
    
    /**
     * [简要描述]：判断当前天是否为指定的天</br>
     * [详细描述]：如dayOfMonth为1，则判断当前天是否为当月的1号</br>
     * [作者]：Aaron(2017-07-26)</br>
     *
     * @param dayOfMonth 月的几号，如：1 即为1号
     * @return true是 false 否
     */
    public static boolean checkIsDayOfMonth(int dayOfMonth)
    {
        Calendar calendar = Calendar.getInstance();
        int currentDay = calendar.get(Calendar.DATE);
        return currentDay == dayOfMonth;
    }
    
    // 获取时间判断是本月的第几周
    public Integer getWeekOfMonth(Date date)
    {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int weekOfMonth = calendar.get(Calendar.WEEK_OF_MONTH);
        return weekOfMonth;
    }
    
    // 获取时间判断是本年的第几周
    public Integer getWeekOfYear(Date date)
    {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int weekOfMonth = calendar.get(Calendar.WEEK_OF_YEAR);
        return weekOfMonth;
    }
    
    // 将string为201709格式的时间转换成2017-09
    public String getDateFormat(String billMonth)
        throws ParseException
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");// 小写的mm表示的是分钟
        Date date = sdf.parse(billMonth);
        DateUtil dateUtil = new DateUtil();
        String dd = dateUtil.convertMonth(date);
        return dd;
    }
    
    // dateYMSimpleDateFormat
    
    // 根据某月的第几周获取该周的周一
    public Date getDateByWeek(Integer year, Integer month, Integer week)
    {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.WEEK_OF_MONTH, week);
        calendar.set(Calendar.DAY_OF_WEEK, 2);
        Date date = calendar.getTime();
        return date;
        
    }
    
    /**
     * 
     * [简要描述]：</br>
     * [详细描述]：将Date转换成毫秒</br>
     * [作者]：Crystal(2017-11-03)</br>
     *
     * @param date
     * @return
     *
     */
    public static Long getDateToMillionSecond(Date date)
    {
        String time = convertWeBankTime(date);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        Long millionSeconds = null;
        try
        {
            millionSeconds = sdf.parse(time).getTime();
        }
        catch (ParseException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return millionSeconds;
    }
    
    /**
     * 
     * [简要描述]：</br>
     * [详细描述]：两个时间之间相差的秒</br>
     * [作者]：Crystal(2017-11-03)</br>
     *
     * @param d1
     * @param d2
     * @return
     *
     */
    public static Long getTimeOfTwoDate(Date d1, Date d2)
    {
        Long date1 = DateUtil.getDateToMillionSecond(d1);
        Long date2 = DateUtil.getDateToMillionSecond(d2);
        Long date3 = date2 - date1;
        Long date4 = date3 / 1000;
        return date4;
    }
    
    /**
     * 
     * [简要描述]：计算两个日期之间的天数</br>
     * [详细描述]：包含边界</br>
     * [作者]：aaron.wang(2017-11-12)</br>
     *
     * @param early 开始日期
     * @param late 结束日期
     * @return
     *
     */
    public static int daysBetween(Date early, Date late)
    {
        
        java.util.Calendar calst = java.util.Calendar.getInstance();
        java.util.Calendar caled = java.util.Calendar.getInstance();
        calst.setTime(early);
        caled.setTime(late);
        // 设置时间为0时
        calst.set(java.util.Calendar.HOUR_OF_DAY, 0);
        calst.set(java.util.Calendar.MINUTE, 0);
        calst.set(java.util.Calendar.SECOND, 0);
        caled.set(java.util.Calendar.HOUR_OF_DAY, 0);
        caled.set(java.util.Calendar.MINUTE, 0);
        caled.set(java.util.Calendar.SECOND, 0);
        caled.add(java.util.Calendar.DAY_OF_YEAR, 1);
        // 得到两个日期相差的天数
        int days = ((int)(caled.getTime().getTime() / 1000) - (int)(calst.getTime().getTime() / 1000)) / 3600 / 24;
        
        return days;
    }
    
    /**
     * 
     * [简要描述]：获取两个日期段的重叠天日期</br>
     * [详细描述]：</br>
     * [作者]：Aaron(2018-06-05)</br>
     *
     * @param start1
     * @param end1
     * @param start2
     * @param end2
     * @return
     * @throws LandseaException
     *
     */
    public static List<String> getAlphalDate(Date start1, Date end1, Date start2, Date end2)
        throws LandseaException
    {
        long btlong = Math.min(start1.getTime(), end1.getTime());// 开始时间
        long otlong = Math.max(start1.getTime(), end1.getTime());// 结束时间
        long stlong = Math.min(start2.getTime(), end2.getTime());// 开始时间
        long edlong = Math.max(start2.getTime(), end2.getTime());// 结束时间
        
        List<String> result = new ArrayList<String>();
        
        // 具体算法如下
        // 首先看是否有包含关系
        if ((stlong >= btlong && stlong <= otlong) || (edlong >= btlong && edlong <= otlong))
        {
            // 一定有重叠部分
            long sblong = stlong >= btlong ? stlong : btlong;
            long eblong = otlong >= edlong ? edlong : otlong;
            result = AmortizationCalculate.displayDay(dateConvertDate(new Date(sblong)),
                dateConvertDate(new Date(eblong)));
        }
        else if ((btlong >= stlong && btlong <= edlong) || (otlong >= stlong && otlong <= edlong))
        {
            // 一定有重叠部分
            long sblong = btlong >= stlong ? btlong : stlong;
            long eblong = edlong >= otlong ? otlong : edlong;
            result = AmortizationCalculate.displayDay(dateConvertDate(new Date(sblong)),
                dateConvertDate(new Date(eblong)));
        }
        
        return result;
    }    
    
 /*  public static void main(String[] args) throws LandseaException
     {
         //int i = DateUtil.differentDaysByMillisecond('2017-11-07 17:47:49',new Date());
         //System.out.println(i);
         
         String s1 = "2018-06-01 12:00:00";
         String s2 = "2018-06-05 12:00:00";
         String s3 = "2018-06-02 12:00:00";
         String s4 = "2018-07-31 12:00:00";
         Date d1 = convertDate(s1);
         Date d2 = convertDate(s2);
         Date d3 = convertDate(s3);
         Date d4 = convertDate(s4);
         //List<String> list = getAlphalDate(d1, d2, d3, d4);
         System.out.println(DateUtil.getInstence().monthBetween(d1,d4));
       //  System.out.println(DateUtil.getInstence().convertYMDDate(DateUtil.getInstence().getLastDayOfLastMonth(d4)));
        // System.out.println("第2期账单".contains("第"));
     }*/
}
